import os
import cv2
import numpy as np


# 显示图片
def cvShow(img):
    cv2.imshow('0', img)
    cv2.waitKey()


Img = cv2.imread("D:\python.code\carRead\car/img3.png")
# cvShow(Img)

# 高斯去燥
Img1 = cv2.GaussianBlur(Img, (3, 3), 0)
# cvShow(Img1)
# 灰度处理
grayImg = cv2.cvtColor(Img1, cv2.COLOR_BGR2GRAY)
# cvShow(grayImg)

# 边缘检测
# Soble算子边缘检测
Sobel_x = cv2.Sobel(grayImg, cv2.CV_16S, 1, 0)
sImg = cv2.convertScaleAbs(Sobel_x)
# cvShow(sImg)

# 自适应阈值处理
ret, img = cv2.threshold(sImg, 0, 255, cv2.THRESH_OTSU)
# cvShow(img)

# 闭运算
# 先膨胀在腐蚀
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (14, 3))
# print(kernelX)
image = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernelX, iterations=2)
# cvShow(image)

# 去除小白点
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19))
# 膨胀，腐蚀
image = cv2.dilate(image, kernelX)
image = cv2.erode(image, kernelX)
# 腐蚀，膨胀
image = cv2.erode(image, kernelY)
image = cv2.dilate(image, kernelY)
# cvShow(image)

# 中值滤波去除噪点
image = cv2.medianBlur(image, 15)
# cvShow(image)

# 轮廓检测
# cv2 RETR_EXTERNAL 表示只检测外轮廓
# cv2.CHAIN_APPROX_SIMPLE压水平方向，垂直方向，对角线方向的元素，只保留该方向的终点坐标，例如一个矩形轮只需4个点来保存轮席信息
contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
Img2 = Img.copy()
cv2.drawContours(Img2, contours, -1, (0, 0, 255), 1)
# cvShow(Img2)

# 筛选车牌位置的轮廓
# 车牌长宽比在3：1到4：1之间

for item in contours:
    # cv2.boundingRect用一个最小的巨型,把形状包起来
    rect = cv2.boundingRect(item)
    x = rect[0]
    y = rect[1]
    weight = rect[2]
    height = rect[3]
    if (weight > (height * 3)) and (weight < (height * 4.5)):
        rectImg = Img[y:y + height, x:x + weight]
        cv2.imwrite('D:\python.code\carRead\license/license_plate.png', rectImg)
        # cvShow(rectImg)
        # 高斯去噪
        carImg = cv2.GaussianBlur(rectImg, (3, 3), 0)
        # cvShow(carImg)
        # 灰度化
        grayCarImg = cv2.cvtColor(carImg, cv2.COLOR_BGR2GRAY)
        # cvShow(grayCarImg)
        # 自适应阈值处理
        ret, image = cv2.threshold(grayCarImg, 0, 255, cv2.THRESH_OTSU)
        # cvShow(image)
        # 膨胀
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
        img = cv2.dilate(image, kernel)
        # cvShow(img)
        # 轮廓检测
        contours1, hierarchy1 = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        img1 = rectImg.copy()
        cv2.drawContours(img1, contours1, -1, (0, 0, 255), 1)
        # cvShow(img1)
        # 筛选出各个字符的位置轮廓
        words = []
        for item in contours1:
            word = []
            rect = cv2.boundingRect(item)
            x = rect[0]
            y = rect[1]
            weight = rect[2]
            height = rect[3]
            word.append(x)
            word.append(y)
            word.append(weight)
            word.append(height)
            words.append(word)
        words = sorted(words, key=lambda s: s[0], reverse=False)
        # print(words)
        i = 0
        for word in words:
            # 根据每个字符的高度进行筛选
            # if (word[3] > (word[2] * 1.5)) and (word[3] < (word[2] * 4.0)):
            if (word[3] > 20) and (word[3] < 50) and (word[2] > 3) and (word[2] < 25):
                i = i + 1
                if i == 8:
                    break
                else:
                    image = rectImg[word[1]:word[1] + word[3], word[0]:word[0] + word[2]]
                    # cvShow(image)
                    # print(word)
                    cv2.imwrite('D:\python.code\carRead\words/' + str(i) + '.png', image)

templates = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
             'W', 'X', 'Y', 'Z',
             '藏', '川', '鄂', '甘', '赣', '贵', '桂', '黑', '沪', '吉', '冀', '津', '晋', '京', '辽', '鲁', '蒙', '闽',
             '宁', '青', '琼', '陕', '苏', '皖', '湘', '新', '渝', '豫', '粤', '云', '浙']


# 读取图片 输入参数是文件名
def read_img(road):
    referImg_list = []
    for filename in os.listdir(road):
        # print(filename)
        referImg_list.append(road + '/' + filename)
    # print(referImg_list)
    return referImg_list


# read_img('D:\python.code\carRead\model')

# 匹配中文
c_words = []
for i in range(34, 65):
    c_word = read_img('./model/' + templates[i])
    c_words.append(c_word)
# print(c_words)

# 读取车牌字符
img = cv2.imread('./words/1.png')
# cvShow(img)
# 高斯去噪
image = cv2.GaussianBlur(img, (3, 3), 0)
# 灰度处理
grayimg = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# cvShow(grayimg)
# 自适应阈值处理
ret, image1 = cv2.threshold(grayimg, 0, 255, cv2.THRESH_OTSU)
# cvShow(image)


best_score = []  # 存取每个省市最高的得分
for c_word in c_words:
    score = []  # 存取的时每个省市的得分
    for word in c_word:
        # 读取模板中的照片
        # formfile()函数读回数据时需要用户指定元素类型，并对数组形状进行适当的修改
        template_img = cv2.imdecode(np.fromfile(word, dtype=np.uint8), 1)
        # 灰度处理
        template_img = cv2.cvtColor(template_img, cv2.COLOR_RGB2GRAY)
        # 自适应阈值处理
        ret, template_img = cv2.threshold(template_img, 0, 255, cv2.THRESH_OTSU)
        # 获取模板的长宽
        height, width = template_img.shape
        # 复制车牌字符图片
        image = image1.copy()
        # 让两者具有相同的长宽
        image = cv2.resize(image, (width, height))
        # 两者进行匹配 TM_CCOEFFF 匹配程度越高，计算出来的值越大
        result = cv2.matchTemplate(image, template_img, cv2.TM_CCOEFF)
        score.append(result[0][0])
    best_score.append(max(score))
# 获取最高得分元素的下标
num = best_score.index(max(best_score))
print(templates[num + 34])

# 识别车牌第二个字母
# 读取图片
img = cv2.imread('./words/2.png')
# cvShow(img01)
# 高斯去噪
img = cv2.GaussianBlur(img, (3, 3), 0)
# 灰度处理
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# cvShow(img)
# 自适应阈值处理
ret, image2 = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)
# cvShow(image2)
# 字母模板匹配
c_words = []
for i in range(10, 34):
    # print(templates[i])
    c_word = read_img('./model/' + templates[i])
    c_words.append(c_word)
# print(c_words)
best_score = []  # 存取每个省市最高的得分
for c_word in c_words:
    score = []  # 存取的时每个省市的得分
    for word in c_word:
        # 读取模板中的照片
        # formfile()函数读回数据时需要用户指定元素类型，并对数组形状进行适当的修改
        template_img = cv2.imdecode(np.fromfile(word, dtype=np.uint8), 1)
        # 灰度处理
        template_img = cv2.cvtColor(template_img, cv2.COLOR_RGB2GRAY)
        # 自适应阈值处理
        ret, template_img = cv2.threshold(template_img, 0, 255, cv2.THRESH_OTSU)
        # 获取模板的长宽
        height, width = template_img.shape
        # 复制车牌字符图片
        image = image2.copy()
        # 让两者具有相同的长宽
        image = cv2.resize(image, (width, height))
        # 两者进行匹配 TM_CCOEFFF 匹配程度越高，计算出来的值越大
        result = cv2.matchTemplate(image, template_img, cv2.TM_CCOEFF)
        score.append(result[0][0])
        # print(word)
    # print(score)
    best_score.append(max(score))
# print(best_score)
# 获取最高得分元素的下标
num = best_score.index(max(best_score))
print(templates[10 + num])


def strOrnum(path):
    # 读取图片
    img = cv2.imread(path)
    # cvShow(img01)
    # 高斯去噪
    img = cv2.GaussianBlur(img, (3, 3), 0)
    # 灰度处理
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # cvShow(img)
    # 自适应阈值处理
    ret, image2 = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)
    # cvShow(image2)
    # 字母模板匹配
    c_words = []
    for i in range(0, 34):
        # print(templates[i])
        c_word = read_img('./model/' + templates[i])
        c_words.append(c_word)
    # print(c_words)
    best_score = []  # 存取每个省市最高的得分
    for c_word in c_words:
        score = []  # 存取的时每个省市的得分
        for word in c_word:
            # 读取模板中的照片
            # formfile()函数读回数据时需要用户指定元素类型，并对数组形状进行适当的修改
            template_img = cv2.imdecode(np.fromfile(word, dtype=np.uint8), 1)
            # 灰度处理
            template_img = cv2.cvtColor(template_img, cv2.COLOR_RGB2GRAY)
            # 自适应阈值处理
            ret, template_img = cv2.threshold(template_img, 0, 255, cv2.THRESH_OTSU)
            # 获取模板的长宽
            height, width = template_img.shape
            # 复制车牌字符图片
            image = image2.copy()
            # 让两者具有相同的长宽
            image = cv2.resize(image, (width, height))
            # 两者进行匹配 TM_CCOEFFF 匹配程度越高，计算出来的值越大
            result = cv2.matchTemplate(image, template_img, cv2.TM_CCOEFF)
            score.append(result[0][0])
        best_score.append(max(score))
    # 获取最高得分元素的下标
    num = best_score.index(max(best_score))
    print(templates[num])


strOrnum('./words/3.png')
strOrnum('./words/4.png')
strOrnum('./words/5.png')
strOrnum('./words/6.png')
strOrnum('./words/7.png')
